package com.taobao.pet.controller;
import com.taobao.basic.util.JsonResult;
import com.taobao.basic.util.LoginContext;
import com.taobao.basic.util.PageList;
import com.taobao.pet.domain.Pet;
import com.taobao.pet.query.PetQuery;
import com.taobao.pet.service.IPetService;
import com.taobao.pet.service.IPetService;
import com.taobao.user.domain.Logininfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController // controller层，返回json格式数据
@RequestMapping("/pet")
@Api(value = "宠物接口类",description = "宠物接口的CRUD操作")
public class PetController {

    @Autowired
    private IPetService PetService;

    /**
     * 查询所有数据
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有")
    public List<Pet> findAll(HttpServletRequest request){

        // 获取登录人信息
        Logininfo logininfo = LoginContext.getLogininfo(request);

        return PetService.loadAll();
    }
    /**
     * 根据id查询单个数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个对象")
    public Pet findOne(@PathVariable("id") Long id){
        return PetService.loadById(id);
    };
    /**
     * 删除操作
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除接口")
    public JsonResult DeleteByID(@PathVariable("id") Long id){
        try {
            PetService.remove(id);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return  JsonResult.me().setMsg("网络加载失败，请稍后重试！！");
        }
    }
    /**
     * 添加或修改操作
     * @param Pet
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加或修改接口")
    public JsonResult updateOrAdd(@RequestBody Pet Pet ){
        try {
            if (Pet.getId() == null){
                PetService.save(Pet);
            }else {
                PetService.update(Pet);
            }
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 分页查询
     * @param query
     * @return
     *
     */
    @PostMapping
    @ApiOperation(value = "分页查询接口")
    // @RequestBody -从请求体中获取数据赋值给PetQuery
    public PageList<Pet> pageQuery(@RequestBody PetQuery query){
        try {
            return PetService.pageQuery(query);
        }catch (Exception e){
            e.printStackTrace();
            return new PageList<Pet>();
        }
    }
    /**
     * 批量删除
     * @param
     * @return
    */
    @PatchMapping()
    @ApiOperation(value = "删除接口")
    public JsonResult patchDel(@RequestBody List<Long> ids){
        try {
            PetService.patchDel(ids);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return  JsonResult.me().setMsg("网络加载失败，请稍后重试！！");
        }
    }
    /**
     * 宠物上架
     * @param
     * @return
     */
    @PostMapping("/onsale")
    @ApiOperation(value = "上架")
    public JsonResult onSale(@RequestBody List<Long> ids){
        try {
            PetService.onSale(ids);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return  JsonResult.me().setMsg("网络加载失败，请稍后重试！！");
        }
    }
    /**
     * 宠物下架
     * @param
     * @return
     */
    @PostMapping("/offsale")
    @ApiOperation(value = "下架")
    public JsonResult offSale(@RequestBody List<Long> ids){
        try {
            PetService.offSale(ids);
            return JsonResult.me();
        }catch (Exception e){
            e.printStackTrace();
            return  JsonResult.me().setMsg("网络加载失败，请稍后重试！！");
        }
    }
}
